package com.medicine.hbp.kankan.cardslide;

import java.util.ArrayList;
import java.util.List;


import org.kymjs.kjframe.http.HttpParams;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.medicine.hbp.http.AppHttpCallBack;
import com.medicine.hbp.http.AppHttps;
import com.medicine.hbp.http.HttpInterface;
import com.medicine.hbp.mob.findVoteArtRandomListVo;
import com.medicine.hbp.mob.findVoteArtRandomVo;
import com.medicine.hbp.sp.UtilsSp;
import com.medicine.hbp.uitl.ToastUtil;
import com.medicine.hbp.R;

/**
 * 卡片Fragment
 *
 * @author xmuSistone
 */
@SuppressLint({ "HandlerLeak", "NewApi", "InflateParams" })
public class CardFragment extends Fragment {

	private CardSlidePanel.CardSwitchListener cardSwitchListener;

	//	private List<CardDataItem> dataList = new ArrayList<CardDataItem>();
	private LinearLayout layout_card;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
							 Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.card_layout, null);
		layout_card = (LinearLayout) rootView.findViewById(R.id.layout_card);
		initView(rootView);
		return rootView;
	}

	@Override
	public void onStart() {
		super.onStart();
		layout_card.setVisibility(View.VISIBLE);
	}

	CardSlidePanel slidePanel;

	private void initView(View rootView) {

		slidePanel = (CardSlidePanel) rootView
				.findViewById(R.id.image_slide_panel);
		// if(UtilsSp.getIsLisk(getActivity())){
		// layout_card.setVisibility(View.GONE);
		// }else{
		// layout_card.setVisibility(View.VISIBLE);
		// }
		layout_card.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				layout_card.setVisibility(View.GONE);
				UtilsSp.setIsLisk(true, getActivity());
			}
		});
		cardSwitchListener = new CardSlidePanel.CardSwitchListener() {

			@Override
			public void onShow(int index) {
				// Log.d("CardFragment", "正在显示-" +
				// dataList.get(index).userName);
			}

			@Override
			public void onCardVanish(int index, int type) {// 0喜欢 1 不喜欢

				if (type == 0) {
					httpPointPraiseArt(info.get(0).artId);
				} else if (type == 1) {
					httpPointNegativeArt(info.get(0).artId);
				}
				// Log.d("CardFragment", "正在消失-" + dataList.get(index).userName
				// + " 消失type=" + type);
			}

			@Override
			public void onItemClick(View cardView, int index) {
				// Log.d("CardFragment", "卡片点击-" +
				// dataList.get(index).userName);
			}
		};
		slidePanel.setCardSwitchListener(cardSwitchListener);

		// prepareDataList();

		http01();
	}

	public List<findVoteArtRandomVo> info = new ArrayList<findVoteArtRandomVo>();

	/**
	 * 投票相关信息
	 */
	private void http01() {
		HttpParams params = new HttpParams();
		AppHttps.HttpPost(getActivity(), HttpInterface.FIND_VOTE_ART_RANDOM,
				params, new AppHttpCallBack<findVoteArtRandomListVo>(
						findVoteArtRandomListVo.class, getActivity()) {
					@Override
					public void onSuccess(findVoteArtRandomListVo result) {
						if (null != result && null != result.info) {
							info.add(result.info);
							findVoteArtRandomVo infos = new findVoteArtRandomVo();
							info.add(infos);
							slidePanel.fillData(info);
						}

					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(getActivity(), errorMsg);
					}
				});

	}

	/**
	 * 投票相关信息
	 */
	private void http02(final boolean is) {
		HttpParams params = new HttpParams();
		AppHttps.HttpPost(getActivity(), HttpInterface.FIND_VOTE_ART_RANDOM,
				params, new AppHttpCallBack<findVoteArtRandomListVo>(
						findVoteArtRandomListVo.class, getActivity()) {
					@Override
					public void onSuccess(findVoteArtRandomListVo result) {
						if (null != result && null != result.info) {
							if (is) {
								info.add(result.info);
								slidePanel.fillData(info);
							} else {
								info.clear();
								info.add(result.info);
								slidePanel.appendData(info);
							}
						}
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(getActivity(), errorMsg);
					}
				});

	}

	/**
	 * 文章差评接口
	 */
	private void httpPointNegativeArt(int id) {
		HttpParams params = new HttpParams();
		params.put("artId", id);// 所属文章ID
		AppHttps.HttpPost(getActivity(), HttpInterface.POINT_NEGATIVE_ART,
				params,
				new AppHttpCallBack<Object>(Object.class, getActivity()) {
					@Override
					public void onSuccess(Object result) {
						http02(false);
						ToastUtil.showShort(getActivity(), "不喜欢");
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(getActivity(), errorMsg);
					}
				});
	}

	/**
	 * 对文章给予点赞功能
	 */
	private void httpPointPraiseArt(int id) {
		HttpParams params = new HttpParams();
		params.put("artId", id);// 所属文章ID
		AppHttps.HttpPost(getActivity(), HttpInterface.POINT_PRAISE_ART,
				params,
				new AppHttpCallBack<Object>(Object.class, getActivity()) {
					@Override
					public void onSuccess(Object result) {
						http02(false);
						ToastUtil.showShort(getActivity(), "喜欢");
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(getActivity(), errorMsg);
					}
				});
	}

}
